package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.Scanner;

/**
 * 新增笔记信息
 */
public class ZengNote {
    public static void main(String[] args) throws Exception {
        Scanner s=new Scanner(System.in);
        System.out.println("请输入要新增的笔记标题");
        String title=s.next();
        System.out.println("请输入要新增的笔记内容");
        String content=s.next();
        //创建连接池
        Properties properties=new Properties();
        properties.load(new FileInputStream("D:\\study\\Java\\YuQue\\src\\druid.properties.properties"));
        //dataSource 就是连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //获取连接
        Connection connection = dataSource.getConnection();
        //sql语句
        String str="INSERT INTO note(author,title,datetime,content,person_id) " +
                "VALUES(?,?,?,?,?)";
        //拉数据
        PreparedStatement preparedStatement = connection.prepareStatement(str);
        //给?赋值
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //将日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd- HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        preparedStatement.setString(1,"五金");
        preparedStatement.setString(2,title);
        preparedStatement.setString(3,format);
        preparedStatement.setString(4,content);
        preparedStatement.setString(5,"1");
        //新增sql语句
        int i = preparedStatement.executeUpdate();
        if(i>0){
            System.out.println("新增成功");
        }else {
            System.out.println("新增失败");
        }
    }
}
